home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifgate / msgidbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-04  |  2.8 KB  |  132 lines

  1. #ifdef HAS_NDBM_H
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <ndbm.h>
  8. #include <fcntl.h>
  9. #include "lutil.h"
  10. #include "xutil.h"
  11. #include "config.h"
  12. #include "ftn.h"
  13.  
  14. static DBM *id_db = NULL;
  15. static int opened = 0;
  16.  
  17. static int fillidb(void);
  18.  
  19. static int init(void)
  20. {
  21.     char buf[128];
  22.     struct stat stbuf1,stbuf2;
  23.     FILE *fp;
  24.     int needbuild;
  25.  
  26.     if ((msgidbm == NULL) || (newslog == NULL)) return -1;
  27.     if (opened == -1) return -1;
  28.     if (stat(newslog,&stbuf1) != 0) return -1;
  29.     if (opened) return 0;
  30.     needbuild=0;
  31.     sprintf(buf,"%s.dir",msgidbm);
  32.     if ((stat(buf,&stbuf2) != 0) ||
  33.         (stbuf1.st_mtime > stbuf2.st_mtime))
  34.     {
  35.         loginf("SEEN-BY database rebuild requested");
  36.         needbuild=1;
  37.         sprintf(buf,"%s.dir",msgidbm);
  38.         fp=fopen(buf,"w");
  39.         if (fp) fclose(fp);
  40.         sprintf(buf,"%s.pag",msgidbm);
  41.         fp=fopen(buf,"w");
  42.         if (fp) fclose(fp);
  43.     }
  44.     if ((id_db=dbm_open(msgidbm,O_RDWR,0600)) == NULL)
  45.     {
  46.         opened = -1;
  47.         return -1;
  48.     }
  49.     opened = 1;
  50.  
  51.     if (needbuild) return fillidb();
  52.     return 0;
  53. }
  54.  
  55. static int fillidb(void)
  56. {
  57.     datum key,val;
  58.     char buf[128],*line=NULL,se[16],*seen=NULL,*p,*q,*pe,*id;
  59.     faddr *fa = NULL;
  60.     FILE *fp;
  61.     int n=0;
  62.  
  63.     if ((fp=fopen(newslog,"r")) == NULL) return -1;
  64.  
  65.     while (!feof(fp))
  66.     {
  67.         while (((line == NULL) || (line[strlen(line)-1] != '\n')) &&
  68.                (fgets(buf,sizeof(buf)-1,fp)))
  69.             line=xstrcat(line,buf);
  70.         if (line == NULL) continue;
  71.         pe=line+strlen(line);
  72.         strtok(line," \t\n");    /* Month */
  73.         strtok(NULL," \t\n");    /* Date */
  74.         strtok(NULL," \t\n");    /* Time */
  75.         strtok(NULL," \t\n");    /* Host */
  76.         if (((p=strtok(NULL," \t\n")) == NULL) ||
  77.             (*p != '+')) continue;
  78.         if ((id=strtok(NULL," \t\n")) == NULL) continue;
  79.         /* some witchkraft with char *q,*pe is needed because */
  80.         /* otherwise strtok called from within parsefaddr will */
  81.         /* interfere with strtok in the while cycle */
  82.         q=NULL;
  83.         while ((p=strtok(q," \t\n")))
  84.         {
  85.             if ((q=p+strlen(p)+1) >= pe) q=NULL;
  86.             debug(8,"try parse %s",p);
  87.             if ((fa=parsefaddr(p)))
  88.             {
  89.                 sprintf(se,"%s ",
  90.                     ascfnode(fa,0x06));
  91.                 seen=xstrcat(seen,se);
  92.                 debug(8,"new seen: \"%s\"",seen);
  93.                 tidy_faddr(fa);
  94.             }
  95.         }
  96.         if (seen)
  97.         {
  98.             key.dptr=id;
  99.             key.dsize=strlen(id);
  100.             val.dptr=seen;
  101.             val.dsize=strlen(seen);
  102.             *(seen+strlen(seen)-1) = '\0';
  103.             if (dbm_store(id_db,key,val,0) != 0)
  104.                 logerr("$cannot store: \"%s\" \"%s\"",id,seen);
  105.             else debug(8,"seen-by for \"%s\" is \"%s\"",id,seen);
  106.             n++;
  107.         }
  108.         if (seen) free(seen);
  109.         seen=NULL;
  110.         if (line) free(line);
  111.         line=NULL;
  112.     }
  113.     loginf("SEEN-BY database now contains %d records",n);
  114.     return 0;
  115. }
  116.  
  117. char *idlookup(msgid)
  118. char *msgid;
  119. {
  120.     datum key,val;
  121.  
  122.     if (init()) return "";
  123.  
  124.     debug(8,"idlookup \"%s\"",msgid);
  125.     key.dptr=msgid;
  126.     key.dsize=strlen(msgid);
  127.     val=dbm_fetch(id_db,key);
  128.     if (val.dptr) return val.dptr;
  129.     else return "";
  130. }
  131. #endif
  132.